package com.xtoad.ecms.common.utils.generate;

import java.text.MessageFormat;
import java.util.Map;

/**
 * 服务类生成器
 *
 * @author xtoad
 * @date 2021/1/19
 */
public class ServiceImpGenerator implements IGenerator {
    /**
     * 根据包路径和类名生成类
     *
     * @param packagePath 包路径
     * @param modelName   类名
     * @return 生成的类
     */
    @Override
    public String generate(String packagePath, String modelName) {
        System.out.println("generate Service start ");
        Map<String, String> classComment = Generator.getClassComment(Generator.modelPackage + "." + modelName);
        StringBuilder sb = new StringBuilder();
        sb.append("/*\r\n")
                .append(" * Copyright  2019-2029  xtoad\r\n")
                .append(" */\r\n")
                .append("package ").append(packagePath).append(".impl;\r\n")
                .append("\r\n")
                .append("import com.xtoad.ecms.baseinfo.converter.{0}Converter;\r\n")
                .append("import com.xtoad.ecms.baseinfo.dto.{0}DTO;\r\n")
                .append("import com.xtoad.ecms.baseinfo.model.{0};\r\n")
                .append("import com.xtoad.ecms.baseinfo.repository.I{0}Repository;\r\n")
                .append("import com.xtoad.ecms.baseinfo.service.I{0}Service;\r\n")
                .append("import com.xtoad.ecms.common.web.exception.BusinessException;\r\n")
                .append("import com.xtoad.ecms.common.web.exception.ResultCodeEnum;\r\n")
                .append("import org.springframework.stereotype.Service;\r\n")
                .append("\r\n")
                .append("import javax.annotation.Resource;\r\n")
                .append("import java.util.List;\r\n")
                .append("import java.util.Optional;\r\n")
                .append("\r\n")
                .append("/**\r\n")
                .append(" * {2}服务实现类\r\n")
                .append(" *\r\n")
                .append(" * @author ").append(Generator.author).append("\r\n")
                .append(" * @date ").append(Generator.dateString).append("\r\n")
                .append(" */\r\n")
                .append("@Service\r\n")
                .append("public class {0}ServiceImpl implements I{0}Service '{'\r\n")
                .append("\r\n")
                .append("    @Resource\r\n")
                .append("    private I{0}Repository {1}Repository;\r\n")
                .append("    @Resource\r\n")
                .append("    private {0}Converter {1}Converter;\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 根据id查询{2}\r\n")
                .append("     *\r\n")
                .append("     * @param id {2}id\r\n")
                .append("     * @return 查询结果\r\n")
                .append("     */\r\n")
                .append("    @Override\r\n")
                .append("    public {0}DTO get{0}ById(final Long id) '{'\r\n")
                .append("        Optional<{0}> {1} = {1}Repository.findById(id);\r\n")
                .append("        if ({1}.isPresent()) '{'\r\n")
                .append("            return {1}Converter.toDto({1}.get());\r\n")
                .append("        '}'\r\n")
                .append("\r\n")
                .append("        throw new BusinessException(ResultCodeEnum.NOT_FOUND);\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 查询全部{2}，不分页\r\n")
                .append("     *\r\n")
                .append("     * @return 查询结果\r\n")
                .append("     */\r\n")
                .append("    @Override\r\n")
                .append("    public List<{0}DTO> getAll{0}() '{'\r\n")
                .append("        List<{0}> all{0}s = this.{1}Repository.findAll();\r\n")
                .append("        return {1}Converter.toDtoList(all{0}s);\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 新增{2}\r\n")
                .append("     *\r\n")
                .append("     * @param {1}DTO 保存对象\r\n")
                .append("     * @return 新增结果\r\n")
                .append("     */\r\n")
                .append("    @Override\r\n")
                .append("    public {0}DTO insert(final {0}DTO {1}DTO) '{'\r\n")
                .append("        {0} {1} = {1}Converter.toModel({1}DTO);\r\n")
                .append("        {1} = {1}Repository.save({1});\r\n")
                .append("\r\n")
                .append("        return {1}Converter.toDto({1});\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 删除{2}\r\n")
                .append("     *\r\n")
                .append("     * @param id 删除对象id\r\n")
                .append("     */\r\n")
                .append("    @Override\r\n")
                .append("    public void deleteById(final Long id) '{'\r\n")
                .append("        {1}Repository.deleteById(id);\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 删除{2}\r\n")
                .append("     *\r\n")
                .append("     * @param {1}DTO 删除对象\r\n")
                .append("     */\r\n")
                .append("    @Override\r\n")
                .append("    public void delete(final {0}DTO {1}DTO) '{'\r\n")
                .append("        {0} {1} = {1}Converter.toModel({1}DTO);\r\n")
                .append("        {1}Repository.delete({1});\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 更新{2}\r\n")
                .append("     *\r\n")
                .append("     * @param {1}DTO 更新对象\r\n")
                .append("     * @return 更新结果\r\n")
                .append("     */\r\n")
                .append("    @Override\r\n")
                .append("    public {0}DTO update(final {0}DTO {1}DTO) '{'\r\n")
                .append("        {0} {1} = {1}Converter.toModel({1}DTO);\r\n")
                .append("        {1} = {1}Repository.save({1});\r\n")
                .append("\r\n")
                .append("        return {1}Converter.toDto({1});\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 批量新增{2}\r\n")
                .append("     *\r\n")
                .append("     * @param {1}DTOList 新增对象\r\n")
                .append("     * @return 新增结果\r\n")
                .append("     */\r\n")
                .append("    @Override\r\n")
                .append("    public List<{0}DTO> batchInsert(final List<{0}DTO> {1}DTOList) '{'\r\n")
                .append("        List<{0}> {1}s = {1}Converter.toModelList({1}DTOList);\r\n")
                .append("        {1}s = {1}Repository.saveAll({1}s);\r\n")
                .append("\r\n")
                .append("        return {1}Converter.toDtoList({1}s);\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 批量删除{2}\r\n")
                .append("     *\r\n")
                .append("     * @param {1}DTOList 删除对象\r\n")
                .append("     */\r\n")
                .append("    @Override\r\n")
                .append("    public void batchDelete(final List<{0}DTO> {1}DTOList) '{'\r\n")
                .append("        List<{0}> {1}s = {1}Converter.toModelList({1}DTOList);\r\n")
                .append("        {1}Repository.deleteInBatch({1}s);\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 批量更新{2}\r\n")
                .append("     *\r\n")
                .append("     * @param {1}DTOList 更新对象\r\n")
                .append("     * @return 更新结果\r\n")
                .append("     */\r\n")
                .append("    @Override\r\n")
                .append("    public List<{0}DTO> batchUpdate(final List<{0}DTO> {1}DTOList) '{'\r\n")
                .append("        List<{0}> {1}s = {1}Converter.toModelList({1}DTOList);\r\n")
                .append("        {1}s = {1}Repository.saveAll({1}s);\r\n")
                .append("\r\n")
                .append("        return {1}Converter.toDtoList({1}s);\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("'}'\r\n");

        System.out.println("generate Service end ");

        return MessageFormat.format(sb.toString(), modelName, Generator.firstCharLower(modelName), classComment.get(modelName));
    }
}
